package cn.hrfax.stf.sdk.util;

import java.io.IOException;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * httpclient 连接工具类
 *
 * @author yongSen.wang
 * @date 2020/6/15 16:24
 */
@Slf4j
public class HttpUtil extends HttpClientPools {

    public static final String CONTENTTYPE_APPLICATION_JSON = "application/json";
    public static final String CONTENTTYPE_TEXT_HTML        = "text/html";

    /**
     * post 提交报文 json 格式
     * @param url
     * @param jsonContent
     * @param charSet
     * @return
     */
    public static String doPost(String url, String jsonContent, String charSet) {
        return doPost(url, jsonContent, charSet, CONTENTTYPE_APPLICATION_JSON);
    }

    /**
     * post 提交报文 json 格式
     * @param url
     * @param jsonContent
     * @param charSet
     * @param contentType
     * @return
     */
    public static String doPost(String url, String jsonContent, String charSet,
                                String contentType) {
        CloseableHttpClient httpClient;
        HttpPost httpPost;
        String result = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = getHttpClient();
            httpPost = new HttpPost(url);
            RequestConfig requestConfig = getRequestConfig();

            httpPost.setConfig(requestConfig);
            //防止乱码
            httpPost.addHeader("Content-type", contentType + "; charset=" + charSet);
            httpPost.setHeader("Accept", contentType);

            //设置参数
            StringEntity s = new StringEntity(jsonContent, charSet);
            s.setContentType(contentType);
            s.setContentEncoding(charSet);
            httpPost.setEntity(s);
            response = httpClient.execute(httpPost);

            if (response != null) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    log.error("http response statusCode:{} ", statusCode);
                    throw new Exception("请求响应失败");
                }

                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charSet);
                }
            }
        } catch (Exception ex) {
            log.error("http  请求异常", ex);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}